import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '../views/index.vue'
import detail from '../views/detail.vue'
import login from '../views/login.vue'
import mine from '../views/mine.vue'
import mycollects from '../views/mycollects.vue'


Vue.use(VueRouter)
const router = new VueRouter({
	routes:[
		{
			path:'/',
			component:index,
			meta:{requiresAuth:false}
		},
		{
			path:'/xq',
			component:detail,
			meta:{requiresAuth:false}
		},
		{
			path:'/login',
			component:login,
			meta:{requiresAuth:false}
		},
		{
			path:'/mine',
			component:mine,
			meta:{requiresAuth:true}
		},
		{
			path:'/mycollects',
			component:mycollects,
			meta:{requiresAuth:false}
		},
	]	
})
router.beforeEach((to, from, next) => {
	// console.log(to)
	// console.log(from)
	// console.log(next)
	
	if(to.meta.requiresAuth){
		let userinfo = JSON.parse(localStorage.getItem('userinfo'))
		if(userinfo===null || userinfo===undefined || !userinfo.islogin){
			router.push({
				path:'/login'
			})
		}else{
			next()
		}
	}else{
		next()
	}
})


export default router;